package com.fm.jbsql.entry;

import java.io.*;
import java.util.*;

//import org.apache.tools.ant.Task;

public class JbsqlTask {
	private String server=null;
	private String user=null;
	private String password=null;
	private String database=null;
	private String driver=null;
	private String charset=null;
	private String sqlfile=null;
	private String sqlout=null;
	private String colsep="\t";
	private String svni_rev=null;
	private String svni_b=null;
	private String svni_a=null;
	private String path=null;
	private String loglevel=null;
	private String genlst=null;
	private String dryrun=null;
	private String logonly=null;
	private String rev=null;
	private String dir1=null;
	private String nolog=null;
	private String errbreak=null;
	private String rowsep="true";
	private String sybase=null;
	//-------STDOUT REDIRECT VARIABLES----
	private String stdout=null;
	private PrintStream stdout_orig=null;
	
	public String getTaskName(){
		return "jbsql";
	}
	private boolean hasValue(String s){
		if(s==null)return false;
		if(s.length()==0 )return false;
		return true;
	}
	
	public void setServer(String s){
		this.server=s;
	}
	public void setUser(String s){
		this.user=s;
	}
	public void setPassword(String s){
		this.password=s;
	}
	public void setDatabase(String s){
		this.database=s;
	}
	public void setDriver(String s){
		this.driver=s;
	}
	public void setCharset(String s){
		this.charset=s;
	}
	public void setSqlfile(String s){
		this.sqlfile=s;
	}
	public void setSqlout(String s){
		this.sqlout=s;
	}
	public void setColsep(String s){
		this.colsep=s;
	}
	public void setSvni_rev(String s){
		this.svni_rev=s;
	}
	public void setSvni_b(String s){
		this.svni_b=s;
	}
	public void setSvni_a(String s){
		this.svni_a=s;
	}
	public void setPath(String s){
		this.path=s;
	}
	public void setLoglevel(String s){
		this.loglevel=s;
	}
	public void setGenlst(String s){
		this.genlst=s;
	}
	public void setDryrun(String s){
		this.dryrun=s;
	}
	public void setLogonly(String s){
		this.logonly=s;
	}
	public void setRev(String s){
		this.rev=s;
	}
	public void setDir1(String s){
		this.dir1=s;
	}
	public void setNolog(String s){
		this.nolog=s;
	}
	public void setErrbreak(String s){
		this.errbreak=s;
	}
	public void setRowsep(String s){
		this.rowsep=s;
	}
	public void setSybase(String s){
		this.sybase=s;
	}
	//----------------------------------------
	public void setStdout(String s){
		this.stdout=s;
	}
	
	public void execute() throws Exception{
		// use of the reference to Project-instance
		//File currDir = new File(".");

		//System.out.println ( "Dir : " + currDir );
		//System.out.println ( "Dir : " + currDir.getCanonicalPath() );
		Vector arg=new Vector();
		if( hasValue(server)  ) arg.add( "-S"+server);
		if( hasValue(user)    ) arg.add( "-U"+user );
		if( password!=null    ) arg.add( "-P"+password );
		if( hasValue(database)) arg.add( "-D"+database );
		if( hasValue(driver)  ) arg.add( "-C"+driver );
		if( hasValue(charset) ) arg.add( "-J"+charset );
		if( hasValue(sqlfile) ) arg.add( "-i"+sqlfile );
		if( hasValue(sqlout)  ) arg.add( "-o"+sqlout );
		if( hasValue(colsep)  ) arg.add( "-col"+colsep );
		if( hasValue(svni_rev)) arg.add( "-rev"+svni_rev );
		if( hasValue(svni_b)  ) arg.add( "-b"+svni_b );
		if( hasValue(svni_a)  ) arg.add( "-a"+svni_a );
		if( hasValue(path)    ) arg.add( "-p"+path );
		if( hasValue(loglevel)) arg.add( "-l"+loglevel );
		if( "true".equals(genlst))  arg.add( "-g" );
		if( "true".equals(dryrun))  arg.add( "-t" );
		if( "true".equals(logonly)) arg.add( "-y" );
		if( "true".equals(rev))     arg.add( "-r" );
		if( "true".equals(dir1))    arg.add( "-d" );
		if( "true".equals(nolog))   arg.add( "-n" );
		if( "true".equals(errbreak))arg.add( "-x" );
		if( "true".equals(rowsep))  arg.add( "-row" );
		
		if( hasValue(sybase) ){
			System.getProperties().setProperty("SYBASE",sybase);
		} else {
			try {
				System.getProperties().setProperty("SYBASE", System.getenv("SYBASE") );
			} catch (Throwable e){}
		}

			
		
		String args[]={""};
		args=(String[])arg.toArray(args);
		//redirect stdout if needed
		if( hasValue(stdout)  ) {
			stdout_orig = System.out;
			System.setOut( new PrintStream (new FileOutputStream(stdout)) );
		}
		
		Main.run( args );
		
		//return stdout if needed
		if( hasValue(stdout)  ) {
			System.setOut(null);
			System.setOut(stdout_orig);
		}
		
	}
}
